Public Class ConnectionPool
    Private _listeners As List(Of Listener)
    Private _connections As List(Of Connection)
    Private _resources As Resources
    ''' <summary>
    ''' Nos devuelve una lista de listeners que se encuentran actualmente registrados
    ''' </summary>
    ''' <returns>List(Of Listener)</returns>
    Public ReadOnly Property Listeners() As List(Of Listener)
        Get
            Return _listeners
        End Get
    End Property
    ''' <summary>
    ''' Devuelve una lista de las conexiones existentes
    ''' </summary>
    ''' <returns>List(Of Connection)</returns>
    Public ReadOnly Property Connections() As List(Of Connection)
        Get
            Return _connections
        End Get
    End Property
    ''' <summary>
    ''' Solicita una conexion utilizando host y port
    ''' </summary>
    ''' <param name="host">IPAddress del host a la cual conectarnos</param>
    ''' <param name="port">Puerto del host al cual conectarnos</param>
    ''' <returns>Connection</returns>
    Public Function requestConnection(ByVal host As Net.IPAddress, ByVal port As Int16, ByVal connectionFactory As ConnectionFactory) As Connection
        Return requestConnection(New Net.IPEndPoint(host, port), connectionFactory)
    End Function
    ''' <summary>
    ''' Solicita una conexion utilizando host y port
    ''' </summary>
    ''' <param name="target">EndPoint del host al cual conectarnos</param>
    ''' <returns>Connection</returns>
    Public Function requestConnection(ByVal target As Net.IPEndPoint, ByVal connectionFactory As ConnectionFactory) As Connection
        Dim newConn As Core.Connection = connectionFactory.createConnection(target.Address, target.Port)
        _connections.Add(newConn)
        'newConn.Connect()
        Return newConn
    End Function
    ''' <summary>
    ''' Registramos un listener para la escucha
    ''' </summary>
    ''' <param name="listener">Listener que se encargara de estar escuchando</param>
    ''' <returns>True si pudo registrarlo. False si ha fallado.</returns>
    Public Function registerListener(ByVal listener As Listener) As Boolean
        _listeners.Add(listener)
        Return True
    End Function
    ''' <summary>
    ''' Elimina el listener de la lista de escucha.
    ''' </summary>
    ''' <param name="listener">Listener que se encargara de estar escuchando</param>
    ''' <returns>True si pudo eliminarlo. False si ha fallado.</returns>
    Public Function unregisterListener(ByVal listener As Listener) As Boolean
        listener.stopListening()
        _listeners.Remove(listener)
        Return True
    End Function
    ''' <summary>
    ''' Cerramos la conexion seleccionada
    ''' </summary>
    ''' <param name="connection">Connection que queremos cerrar.</param>
    ''' <returns>True si pudo cerrarlo. False si ha fallado.</returns>
    Public Function closeConnection(ByVal connection As Connection) As Boolean
        connection.close()
        _connections.Remove(connection)

    End Function

    Public Sub New(ByVal resources As Resources)
        _resources = resources
        _connections = New List(Of Connection)
        _listeners = New List(Of Listener)
    End Sub
End Class
